/**************************************************************************** 
* 
* Copyright (c) 2023  C*Core -   All Rights Reserved  
* 
* THIS SOFTWARE IS DISTRIBUTED "AS IS," AND ALL WARRANTIES ARE DISCLAIMED, 
* INCLUDING MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* 
* PROJECT     : CCFC2011BC           
* DESCRIPTION : SWT demo code
* HISTORY     : Initial version.
* @file     swt_demo.c
* @version  1.1
* @date     2023 - 02 - 20
* @brief    Initial version.
*
*****************************************************************************/
#include "swt_demo.h"
#include "stm_lld.h"
#include "led_hw.h"
#include "console.h"
#include "sys.h"
#include "intc_lld.h"
#include "wkpu_lld.h"

/*swt0 callback*/
static void SWT_Callback(const SWTDriver *swtd)
{
    /* call related callback */
    (void)PSPRINTF("SWT0 Reset happen! SWT CNT = %d \r\n", swtd->swtp->CO.R);
    LED_Set(LED3, 1);
    LED_Set(LED1, 0);
    delayms(1000);
}

/**
 * @brief   PIT configuration structure
 * @param  time_out value  
 * @param  window value
 * @param  Channel callback function 
*/
static SWT_Config swt0_config[1] ={
    {1000u, 100u, SWT_Callback},
};

/*******************************************************************************
 * @brief      SWT_Test_Case1(uint32_t t_time)
 * @param[in]  time     value of start time
 *             channel      number of channel
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
uint32_t SWT_Test_Case1(uint32_t t_time)
{
    /* SWT driver initialization */
    SWT_LLD_Init(&SWTD1, swt0_config);
    /* Set time_out value */
    SWT_LLD_SetTimeOutPeriod(&SWTD1, t_time);
    /* Enable SWT */
    SWT_LLD_ModuleEnable(&SWTD1);
    (void)PSPRINTF("\n SWT Test End!\n");
    return 0;
}

/*******************************************************************************
 * @brief      SWT_Test_Case2(uint32_t t_time)
 * @param[in]  t_time           value of start time
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
uint32_t SWT_Test_Case2(uint32_t t_time)
{
    uint8_t t_loop;
    /* swt driver initialization */
    SWT_LLD_Init(&SWTD1, swt0_config);
    /* Set time_out value */
    SWT_LLD_SetTimeOutPeriod(&SWTD1, t_time);
    /* Enable SWT */
    SWT_LLD_ModuleEnable(&SWTD1);
    for (t_loop = 0u; t_loop < 10u; t_loop++)
    {
        /* LED2 ON */
        LED2_IO_Set(1);
        (void)STM_LLD_Delayms(50);
        /* LED2 OFF */
        LED2_IO_Set(0);
        (void)STM_LLD_Delayms((t_time/2u));
        /*Fixed sequence feed dog */
        SWT_LLD_KeepAlive(&SWTD1);
    }
    return 0;
}


/*******************************************************************************
 * @brief      SWT_Test_Case2(uint32_t t_time)
 * @param[in]  t_time           value of start time
 *             t_window_time    value of window time
 *             state            number of case
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
uint32_t SWT_Test_Case3(uint32_t t_time, uint32_t t_window_time, uint8_t state)/* PRQA S 1505*/
{
    uint8_t t_loop;
    /* SWT driver initialization */
    SWT_LLD_Init(&SWTD1, swt0_config);
    /* Set time out period */
    SWT_LLD_SetTimeOutPeriod(&SWTD1, t_time);
    /* Set way of feeding dog */
    SWT_LLD_SetKeyedService(&SWTD1, 1);
    /* Set SWT mode */
    SWT_LLD_SetMode(&SWTD1, 1);
    /* Set SWT interrupt mode */
    SWT_LLD_SetInterruptMode(&SWTD1, 1);
    /* Set window period */
    SWT_LLD_SetWindowTime(&SWTD1, t_window_time);
    /* Enable SWT */
    SWT_LLD_ModuleEnable(&SWTD1);
    switch (state)
    {
        case((uint8_t)feed_dog):
            for (t_loop = 0u; t_loop < 10u; t_loop++)
            {
                /* LED2 ON */
                LED2_IO_Set(1);
                (void)STM_LLD_Delayms(50);
                /* LED2 OFF */
                LED2_IO_Set(0);
                (void)STM_LLD_Delayms(t_time - t_window_time);
                /*Random sequence feed dog */
                SWT_LLD_RandomKeepAlive(&SWTD1);
            }
            break;
        case((uint8_t)no_feed_dog):
            break;
        case((uint8_t)feed_error_key):
            for (t_loop = 0u; t_loop < 10u; t_loop++)
            {
                /* LED2 ON */
                LED2_IO_Set(1);
                (void)STM_LLD_Delayms(50);
                /* LED2 OFF */
                LED2_IO_Set(0);
                (void)STM_LLD_Delayms(50);
                /*Random sequence feed dog */
                SWT_LLD_RandomKeepAlive(&SWTD1);
            }
            break;
        default:
            break;
    }
    (void)PSPRINTF("\n SWT Test End!\n");
    return 0;
}

/*******************************************************************************
 * @brief      SWT_Test_Case4 SWT freeze mode test
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
uint32_t SWT_Test_Case4(void)
{
    /* SWT driver initialization */
    SWT_LLD_Init(&SWTD1, swt0_config);
    /* Set freeze mode */
    SWT_LLD_FreezeMode(&SWTD1, 1);
    /* Enable SWT */
    SWT_LLD_ModuleEnable(&SWTD1);
    (void)PSPRINTF("\n SWT Test End!\n");
    return 0;
}

/*******************************************************************************
 * @brief      SWT_Test_Case5 SWT stop mode test
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
uint32_t SWT_Test_Case5(void)
{
    ME.MER.R = 0x000025FFu;
    /* STM delay function init */
    (void)STM_LLD_DelayInit();
    (void)STM_LLD_Delayms(3000);
    /* swt driver initialization */
    SWT_LLD_Init(&SWTD1, swt0_config);
    /* Set Stop mode */
    SWT_LLD_StopMode(&SWTD1, 1);
    /* Enable SWT */
    SWT_LLD_ModuleEnable(&SWTD1);
    (void)STM_LLD_Delayms(500);
    /* system switch into STOP mode */
    MC_ME_ModeSwitch(ME_MODE_STOP);
    (void)PSPRINTF("\n SWT Test End!\n");
    return 0;
}

/*******************************************************************************
 * @brief      SWT_TEST_Demo
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void SWT_TEST_Demo(void)
{
    (void)PSPRINTF("\n SWT Test Start!\n");
    /* Set the current priority */
    INTC_CPR_Init((uint8_t)CPR_PRI_0);
    (void)STM_LLD_DelayInit();
    /* Function: normal watch dog,no feed dog */
#if SWT_CASE1
    (void)SWT_Test_Case1(2000);
#endif

    /* Function: normal watch dog,fixed sequence feed dog */
#if SWT_CASE2
    (void)SWT_Test_Case2(2000);
#endif

    /* Function: window watch dog ,randow sequence feed dog, interrupt then reset */
#if SWT_CASE3
    (void)SWT_Test_Case3(2000u, 200u, (uint8_t)feed_dog);
#endif

    /* Function: SWT freeze mode test */
#if SWT_CASE4
    (void)SWT_Test_Case4();
#endif

    /* Function: SWT STOP mode test */
#if SWT_CASE5
    (void)SWT_Test_Case5();
#endif
}


